//
//  ChartViewController.swift
//  HSStockChartDemo
//
//  Created by Hanson on 16/9/6.
//  Copyright © 2016年 hanson. All rights reserved.
//

import UIKit
import SwiftyJSON

enum HSChartType: Int {
    case timeLineForDay
//    case timeLineForFiveday
    case kLineForDay
    case kLineForWeek
    case kLineForMonth
}

class ChartViewController: UIViewController {
    
    var chartType: HSChartType = .timeLineForDay
    var timeLineView: HSTimeLine?
    var chartRect: CGRect = CGRect.zero
    var isLandscapeMode: Bool = false
    var timer:Timer?
    var stockCode:String?

    
    // MARK: - Life Circle
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpViewController(stockCode: "000030")
//        print(self.view.frame)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewWillAppear(_ animated: Bool) {
//        print(self.view.frame)
    }
    
    override func viewDidLayoutSubviews() {
//        print(self.view.frame)
    }
    
    override func viewDidAppear(_ animated: Bool) {
//        print(self.view.frame)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.timer?.invalidate()
    }
    
    // MARK: - Function
    
    func getTimeChartData(stockBasicInfo:HSStockBasicInfoModel,stockCode:String){
//                self.timeLineView?.dataT = [HSTimeLineModel]()
        weak var weakSelf : ChartViewController? = self
                let block = { (result:TimeChartResult?) in
                    if let re = result{
                        weakSelf?.timeLineView?.dataT = HSTimeLineModel.getTimeLineModelArray(result: re, type: (weakSelf?.chartType)!, basicInfo: stockBasicInfo)
                        weakSelf?.timeLineView!.isUserInteractionEnabled = true
                        weakSelf?.timeLineView?.tag = (weakSelf?.chartType.rawValue)!
                        weakSelf?.timeLineView?.isLandscapeMode = (weakSelf?.isLandscapeMode)!
                        weakSelf?.view.addSubview((weakSelf?.timeLineView)!)
//                        有用
                        weakSelf?.timeLineView?.drawTimeLineChart()
                    }
//                        weakSelf?.timeLineView?.drawTimeLineChart()
                    }
        if(self.timer == nil){
            self.timer = Timer.scheduledTimer(withTimeInterval: requestTimeInterval, repeats: true, block: { _ in
            Utils.AlamofireAsynchronousGETTimeChartData(stockCode: stockCode, callBack: block)
        })
        }
        else{
            self.timer = nil
            self.timer = Timer.scheduledTimer(withTimeInterval: requestTimeInterval, repeats: true, block: { _ in
                Utils.AlamofireAsynchronousGETTimeChartData(stockCode: stockCode, callBack: block)
            })
        }
       
    }
//    func getKChartData(stockBasicInfo:HSStockBasicInfoModel,stockCode:String){
//        //                self.timeLineView?.dataT = [HSTimeLineModel]()
//        weak var weakSelf : ChartViewController? = self
//        let block = { (result:TimeChartResult?) in
//            if let re = result{
//                weakSelf?.timeLineView?.dataT = HSTimeLineModel.getTimeLineModelArray(result: re, type: (weakSelf?.chartType)!, basicInfo: stockBasicInfo)
//                weakSelf?.timeLineView!.isUserInteractionEnabled = true
//                weakSelf?.timeLineView?.tag = (weakSelf?.chartType.rawValue)!
//                weakSelf?.timeLineView?.isLandscapeMode = (weakSelf?.isLandscapeMode)!
//                weakSelf?.view.addSubview((weakSelf?.timeLineView)!)
//                //                        有用
//                weakSelf?.timeLineView?.drawTimeLineChart()
//            }
//            //                        weakSelf?.timeLineView?.drawTimeLineChart()
//        }
//        if(self.timer == nil){
//            self.timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { _ in
//                Utils.AlamofireAsynchronousGETTimeChartData(stockCode: stockCode, callBack: block)
//            })
//        }
//        else{
//            self.timer = nil
//            self.timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { _ in
//                Utils.AlamofireAsynchronousGETTimeChartData(stockCode: stockCode, callBack: block)
//            })
//        }
//
//    }
    func setUpViewController(stockCode:String) {
        let stockBasicInfo = HSStockBasicInfoModel.getStockBasicInfoModel(getJsonDataFromFile("SZ300033"))
        
        switch chartType {
            
        case .timeLineForDay:
            
            timeLineView = HSTimeLine(frame: chartRect)
            getTimeChartData(stockBasicInfo: stockBasicInfo, stockCode: stockCode)
           
            

            
        case .kLineForDay:
            let stockChartView = HSKLineView(frame: chartRect, kLineType: .kLineForDay)
            stockChartView.tag = chartType.rawValue
            stockChartView.isLandscapeMode = self.isLandscapeMode
            self.view.addSubview(stockChartView)
            
        case .kLineForWeek:
            let stockChartView = HSKLineView(frame: chartRect, kLineType: .kLineForWeek)
            stockChartView.tag = chartType.rawValue
            stockChartView.isLandscapeMode = self.isLandscapeMode
            self.view.addSubview(stockChartView)
            
        case .kLineForMonth:
            let stockChartView = HSKLineView(frame: chartRect, kLineType: .kLineForMonth)
            stockChartView.tag = chartType.rawValue
            stockChartView.isLandscapeMode = self.isLandscapeMode
            self.view.addSubview(stockChartView)
        }
    }
    
    func getJsonDataFromFile(_ fileName: String) -> JSON {
        let pathForResource = Bundle.main.path(forResource: fileName, ofType: "json")
        let content =  try? String(contentsOfFile: pathForResource!, encoding: String.Encoding.utf8)
        let jsonContent = content!.data(using: String.Encoding.utf8)
        let jdata = try? JSON(data: jsonContent!)
        return jdata!
    }
    func createTimeLineDataSource(){
        
    }
}
